const Koa = require('koa');//导入koa模块
const {bodyParser} = require('@koa/bodyparser');//导入koa-const Router = require('@koa/router');
const Router = require('@koa/router');//导入koa-router模块
const serve = require('koa-static');
const app = new Koa();//创建一个Koa对象表示web应用本身
const userRouter = require('./routes/user');//导入用户子路由模块
const jindianRouter = require('./routes/jingdian');//导入景点子路由模块
const mount = require('koa-mount');
const cors = require('@koa/cors');
const path = require('path');
const { sequelize } = require('./databases'); // 导入 sequelize 实例和 DataTypes



(async () => {
  await sequelize.sync({ force: false, alter: true });
    console.log('All models were synchronized successfully.');
})()


app.use(bodyParser());//使用bodyParser中间件,解析请求体
//托管静态资源文件123456
app.use(mount('/static',serve(path.join(__dirname,'./static'))));

//koa跨域配置123
app.use(cors({
  origin(ctx) {
    return '*';
  },
}));

// console.log(path.join(__dirname,'./static'))

const router = new Router();//实例化主路由对象
router.use(userRouter.routes())//注册用户路由
router.use(jindianRouter.routes())//注册景点路由
app.use(router.routes())//注册主路由
app.use(router.allowedMethods())//允许使用所有的HTTP方法

//挂载中间件middleware
// app.use(async (ctx,next) => {
//   ctx.body = {
//     title:'hello koa',
//     result:ctx.request.body
//   }
// });


app.listen(8599);//服务器监听在3000端口
console.log('server is running at http://localhost:8599');